package com.easyban.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.easyban.common.R;
import com.easyban.entity.Product;
import com.easyban.entity.User;
import com.easyban.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/product")
@Slf4j
public class ProductController {
    @Autowired
    private ProductService productService;

    @PostMapping
    public R<String> addProduct(@RequestBody Product product) {
        productService.save(product);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Product>> getProductPage(int page, int pageSize, String name) {
        log.info("执行分页查询....");
        //构造分页构造器
        Page<Product> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        if (StringUtils.isNoneEmpty(name)) {
            queryWrapper.like(Product::getName, name)
                    .or().like(Product::getPrice, name);
        }
        //添加排序条件
        queryWrapper.orderByDesc(Product::getSubId);

        //执行查询
        productService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("/subPage")
    public R<Page<Product>> getProductSubPage(int page, int pageSize, String name) {
        log.info("执行分页查询....");
        //构造分页构造器
        Page<Product> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        Long subId = Long.valueOf(name);
        //添加条件
        if (StringUtils.isNoneEmpty(name)) {
            queryWrapper.eq(Product::getSubId, subId);
        }else {
            return R.error("查询失败");
        }
        //添加排序条件
        queryWrapper.orderByDesc(Product::getSubId);

        //执行查询
        productService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    @GetMapping("/{id}")
    private R<Product> getProductById(@PathVariable String id) {
        Product product = productService.getById(id);
        return R.success(product);
    }

    @DeleteMapping("/{id}")
    public R<String> deleteProduct(@PathVariable String id) {
        productService.removeById(id);

        return R.success("删除成功");
    }

    @PutMapping
    public R<String> updateProduct(@RequestBody Product product) {
        productService.updateById(product);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Product>> getRandomSixProducts() {
        List<Product> list = productService.list();
        //Collections.shuffle(list);
        //list.size() > 8 ? list.subList(0, 8) : list
        return R.success(list);
    }

    @GetMapping("/getName/{id}")
    public R<String> getProductName(@PathVariable String id) {
        Product product = productService.getById(id);
        return R.success(product.getName());
    }

    @GetMapping("/getPhoto/{id}")
    public R<String> getProductPhoto(@PathVariable String id) {
        Product product = productService.getById(id);
        return R.success(product.getPhoto());
    }
}
